package com.xdkj.utils;

import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.errors.MinioException;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * @author joke
 * @version 1.0
 * @date 2021/11/25-22:13
 */
public class MinioUtil {
    public static final String HOST = "http://192.168.59.135:9000";
    public static final String USERNAME = "admin";
    public static final String PASSWORD = "12345678";
    public static final String BUCKET = "xdkj-edu";

    public static MinioClient getClient() {
        MinioClient minioClient = null;
        try {
            // Create a minioClient with the MinIO server playground, its access key and secret key.
            minioClient =
                    MinioClient.builder()
                            .endpoint(HOST)
                            .credentials(USERNAME, PASSWORD)
                            .build();

            // Make 'asiatrip' bucket if not exist.
            boolean found =
                    minioClient.bucketExists(BucketExistsArgs.builder().bucket(BUCKET).build());
            if (!found) {
                // Make a new bucket called 'asiatrip'.
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(BUCKET).build());
            } else {
                System.out.println("Bucket 'asiatrip' already exists.");
            }

        } catch (MinioException e) {
            System.out.println("Error occurred: " + e);
            System.out.println("HTTP trace: " + e.httpTrace());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }
        return minioClient;
    }
}
